// pages/baogong/index.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    printCode:'',
    productDetailId:'',
    correlation_order:'',
    detail:{},
    mdId:'',
    hgNum:'',
    gfNum:0,
    lfNum:0,
    processText:'',
    processValue:[],
    processId:'',
    processData:[],
    jjText:'个人',
    jjValue:['个人'],
    jjId:'个人',
    jjData:[{label:'个人',value:'个人'},{label:'班组',value:'班组'}],
    bgText:'',
    bgValue:[],
    bgId:'',
    bgData:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getBg();
    let userInfo=wx.getStorageSync('userInfo');
    this.setData({
      bgId:userInfo.id,
      bgText:userInfo.name,
      bgValue:[userInfo.id]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  processPicker(){
    this.setData({processVisible:true})
  },
  jjPicker(){
    this.setData({jjVisible:true}) 
  },
  bgPicker(){
    this.setData({bgVisible:true}) 
  },
  selectProcess(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
    });
    console.log('data',this.data)
  },
  selectJjfs(e){
    console.log('eee',e)
    let pickName = e.currentTarget.dataset.key;
      let pickValue = e.detail.value;
      console.log('dddddd',e.detail.value)
    this.setData({
      [`${pickName}Visible`]: false,
      [`${pickName}Value`]:  pickValue,
      [`${pickName}Id`]:  pickValue.join(','),
      [`${pickName}Text`]: e.detail.label.join(' '),
      bgText:'',
      bgId:'',
      bgValue:[]
    });
    this.getBg();
  },
  onPickerCancel(e){
    const { key } = e.currentTarget.dataset.key;
  
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  setCode(e){
    this.setData({printCode:e.detail.value})
  },
  burD(e){
    console.log('eee');
    this.setData({printCode:e.detail.value})
    this.getInfo();
  },
  scanCode(){
    let that=this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
    console.log('scr',res)
        if (res.errMsg === "scanCode:ok") {
          let str=res.result;
          let printCode=app.common.getQueryString(str,'printCode');
          let id=app.common.getQueryString(str,'id');
          that.setData({printCode:printCode,productDetailId:id})
          that.getInfo();
        }
      }
    })
  },

  getInfo(){
    let that=this;
    that.setData({detail:{}})
    let printCode=that.data.printCode;
    console.log('dd',that.data);
    if(printCode==''){
      return false;
    }
    let param={printCode:printCode};
    app.common.http_post('/miniProgram/Other/getBgInfo',param,function(res){
      console.log('res',res)
          let data=res.data;
          console.log('wlData',data);
      that.setData({detail:data.mdInfo,processData:data.list,mdId:data.mdInfo.material_dossier_id,correlation_order:data.mdInfo.production_plan_id})
        })
      },
      getBg(){
        let that=this;
        let jjfs=that.data.jjId;
        if(jjfs==''){
          return false;
        }
        let param={type:jjfs};
        console.log('aaa')
        app.common.http_post('/miniProgram/Other/getPricingMode',param,function(res){
          console.log('wwrwerwrwerrwr',res)
              let data=res.data;
              that.setData({bgData:data})
            })
      },

      onsure(){
        let that=this;
        let mdId=that.data.mdId;
        let processId=that.data.processId;
        let jjId=that.data.jjId;
        let bgId=that.data.bgId;
        let hgNum=that.data.hgNum;
        let gfNum=that.data.gfNum;
        let lfNum=that.data.lfNum;
        let correlation_order=that.data.correlation_order;
        console.log(that.data)
        if(hgNum<=0||hgNum==''||lfNum<0||lfNum<0){
          app.common.alert('参数有误')
          return false
        }
        let param={
        correlation_order:correlation_order,
         material_dossier_id:mdId, 
         craft_detail_id:processId,
          pricing_mode:jjId, 
          u_g_id:bgId, 
           qualifiedNum:hgNum,
          passNum:gfNum,
           wasteNum:lfNum
        }
        app.common.showLoading()
        app.common.http_post('/miniProgram/ProcessReporting/onSure',param,function(res){
          app.common.hideLoading();
          app.common.alert(res.msg)
     setTimeout(function(){
       app.common.go_back();
     },500)
          
            })
      },
      sethgNum(e){
        this.setData({hgNum:e.detail.value})
      },
      setgfNum(e){
        this.setData({gfNum:e.detail.value})
      },
      setlfNum(e){
        this.setData({lfNum:e.detail.value})
      },
})